<?php
/**
 * 会员前台管理中心、账号管理、收藏操作类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('mbauth','member');
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class mobile extends mbauth {
	
	function __construct() {
		parent::__construct();
		$this->db =  pc_base::load_model('member_model');
	}

	public function init() {
		$memberinfo = $this->memberinfo;

		include template('member', 'mindex');
	}
	
	public function login() {
		
		$wxid = isset($_REQUEST['wxid']) ? $_REQUEST['wxid'] : '';	
		$shopid = isset($_REQUEST['shopid']) ? $_REQUEST['shopid'] : '';	
		
		if($_POST['dologin']){
			$phone = isset($_REQUEST['mobile']) ? safe_replace($_REQUEST['mobile']) : exit('error! No phone .');
			$code = isset($_REQUEST['code']) ? intval($_REQUEST['code']) : exit('error! No code .');	
		
			//查询帐号
			$r = $this->db->get_one(array('username'=>$phone));
			if(!$r){
				m_showmessage('号码不存在', 'index.php?m=member&c=mobile&a=login');
			}else{
				if($r['code']==$code){
				
					if($wxid!=='' && $shopid!==''){
						$wxdb = pc_base::load_model('wxuser_model');
						$iswx = $wxdb->get_one(array('wx_mobile'=>$phone,'wx_id'=>$wxid));
						if(!$iswx){
							$wxdb->insert(array('wx_mobile'=>$phone,'wx_id'=>$wxid,'wx_addtime'=>SYS_TIME,'wx_shopid'=>$shopid));
						}
					}
/* 
						//通过模型获取会员信息					
						require_once CACHE_MODEL_PATH.'member_input.class.php';
						require_once CACHE_MODEL_PATH.'member_update.class.php';
						$member_input = new member_input($r['modelid']);
						$user_model_info = $member_input->get($otherinfo);
						$user_model_info['userid'] = $r['userid'];

						//插入会员模型数据
						$this->db->set_model($r['modelid']);
						$this->db->insert($user_model_info); */
					
						$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key'));
						$phpcms_auth = sys_auth($r['userid']."\t".$r['password'], 'ENCODE', $phpcms_auth_key);
						param::set_cookie('auth', $phpcms_auth);
						
						m_showmessage('登录成功', 'index.php?m=member&c=mobile');
					
					
				}else{
					m_showmessage('验证失败!请重新验证!', 'index.php?m=member&c=mobile&a=login');
				}
			}
		}
		
		include template('member', 'mb_login');
	}
	
	public function mycar() {
		$memberinfo = $this->memberinfo;

		include template('member', 'mmycar');
	}

	
	public function mycard() {
		$memberinfo = $this->memberinfo;
		
		include template('member', 'mcard');
	}
	
	public function mymessage() {
		$memberinfo = $this->memberinfo;

		include template('member', 'mmymessage');
	}
	
	public function messcon() {
		$memberinfo = $this->memberinfo;
		$message = pc_base::load_model('message_model');
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : m_showmessage('无ID', 'index.php');
		$messcon = $message->get_one(array('messageid'=>$id));
		include template('member', 'mmesscon');
	}
	
	public function useque() {
		$memberinfo = $this->memberinfo;
		$pay_temp = pc_base::load_model('pay_temp_model');
		$is_pay = $pay_temp->get_one(array('userid'=>$memberinfo['userid'],'is_pay'=>0));
		if($is_pay){
			echo 'suss'.$is_pay['id'];
		}
	}
	public function usepay() {
		$memberinfo = $this->memberinfo;
		$payid = isset($_REQUEST['payid']) ? intval($_REQUEST['payid']) : m_showmessage('无订单ID', 'index.php?m=member&c=mobile&a=mycard');

		$pay_temp = pc_base::load_model('pay_temp_model');
		$paydata = $pay_temp->get_one(array('userid'=>$memberinfo['userid'],'id'=>$payid,'is_pay'=>0));
			
		$this->db->set_model(13);
		$shopdata = $this->db->get_one(array('userid'=>$paydata['shopid']));
		
		include template('member', 'musepay');
	}
	public function paydone() {
		$memberinfo = $this->memberinfo;
		$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'ok';
		$payid = isset($_REQUEST['payid']) ? intval($_REQUEST['payid']) : m_showmessage('无订单或订单已经完成!', 'index.php?m=member&c=mobile&a=mycard');
		
		
		$pay_temp = pc_base::load_model('pay_temp_model');
		$paydata = $pay_temp->get_one(array('userid'=>$memberinfo['userid'],'id'=>$payid,'is_pay'=>0));
		
		if($paydata){ 
			$shopdata = $this->db->query("SELECT * FROM phpcms_member a,phpcms_member_shan b WHERE a.userid=b.userid AND  a.userid=".$paydata['shopid']);   //地区
			$shopdata  =mysql_fetch_assoc($shopdata);
			if(!$shopdata) m_showmessage(L('商户不存在!'));

			if($type=='ok'){
				if($paydata['price']>$memberinfo['amount'])  m_showmessage('您当前的余额不足以支付该订单,请先充值后才能进行消费', 'index.php?m=member&c=mobile');
				
				$pay_temp->update(array('is_pay'=>1),array('id'=>$payid));
				$nowmoney = $shopdata['amount']+$paydata['price'];
				$this->db->update(array('amount'=>$nowmoney), array('userid'=>$paydata['shopid']));

				pc_base::load_app_class('spend','pay',0);
				spend::amount($paydata['price'], $paydata['paydes'], $memberinfo['userid'], $memberinfo['username'],$shopdata['userid'],$shopdata['name']);
				
				
				$message_db = pc_base::load_model('message_model');
				$username= $shopdata['username'];
				$tousername =$memberinfo['username'];
				$subject = '您好,您在'.$shopdata['name'].'消费了'.$paydata['paydes'];
				$content = '您好,您在'.$shopdata['name'].'消费了"'.$paydata['paydes'].'",共计'.$paydata['price'].'元,谢谢惠顾!';
				$message_db->add_message($tousername,$username,$subject,$content,true);
				//
				$smscon = '尊敬的会员：您刚于'.date('Y-m-d H:i:s',SYS_TIME).'在'.$shopdata['name'].'消费'.$paydata['price'].'元，余额'.($memberinfo['amount']-$paydata['price']).'元。';
				sendsms($memberinfo['mobile'],$smscon);
				
				echo $smscon;
				exit;
				
				
				include template('member', 'mpaydone');
			}else{
				$pay_temp->update(array('is_pay'=>-1),array('id'=>$payid));
				m_showmessage('交易已取消', 'index.php?m=member&c=mobile');
			}
		}else{
			m_showmessage('无订单ID', 'index.php?m=member&c=mobile');
		}
		
	}
	
	public function paylog() {
		$memberinfo = $this->memberinfo;

			
		
		include template('member', 'mpaylog');
	}
	public function review() {
		$memberinfo = $this->memberinfo;
		$pay_spend = pc_base::load_model('pay_spend_model');
		$review_db = pc_base::load_model('review_model');
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : m_showmessage('找不到该订单ID!');
		$stat = isset($_REQUEST['stat']) ? intval($_REQUEST['stat']) : 0;
		$isorder = $pay_spend->get_one(array('id'=>$id));
		if(!$isorder){ m_showmessage('找不到该订单ID!');}
		
		$shopdata = $this->db->query("SELECT * FROM phpcms_member_shan  WHERE userid=".$isorder['op_userid']); 
		$shopdata  =mysql_fetch_assoc($shopdata);
		
		if(isset($_POST['dosubmit'])) {
			$viewdata['user_id']  = $memberinfo['userid'];
			$viewdata['shop_id']  = $isorder['op_userid'];
			$viewdata['order_id']  = $isorder['id'];
			$viewdata['time']  = SYS_TIME;
			$viewdata['content']  = isset($_REQUEST['content']) ? $_REQUEST['content'] : m_showmessage(L('没有内容!'));
			$viewdata['good']  = $stat;
			
			$is_review = $review_db->get_one(array('order_id'=>$isorder['id']));
			if($is_review){
				m_showmessage(L('已提交过点评!'));
			}else{
				$review_db->insert($viewdata);
			}
			m_showmessage(L('提交成功!'),APP_PATH.'index.php?m=member&c=mobile');
		}else{
		
			$is_review = $review_db->get_one(array('order_id'=>$isorder['id']));
			include template('member', 'mreview');
		}

	}
	
	public function chargelog() {
		$memberinfo = $this->memberinfo;
		
		include template('member', 'mchargelog');
	}
	
	public function yuyue() {	
		$memberinfo = $this->memberinfo;
		$pay_spend = pc_base::load_model('pay_spend_model');
		$yuyue_db = pc_base::load_model('yuyue_model');
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : m_showmessage('找不到该商家ID!');
		$shopdata = $this->db->query("SELECT * FROM phpcms_member_shan  WHERE userid=".$id); 
		$shopdata  =mysql_fetch_assoc($shopdata);
		if(!$shopdata){ m_showmessage('找不到该商家ID!');}
		

		
		if(isset($_POST['dosubmit'])) {
			$time = isset($_REQUEST['time']) ? $_REQUEST['time'] : m_showmessage('没有选时间!');
		
		
			$viewdata['userid']  = $memberinfo['userid'];
			$viewdata['shopid']  = $id;
			$viewdata['time']  = $time;
			$viewdata['content']  = isset($_REQUEST['content']) ? $_REQUEST['content'] : m_showmessage(L('没有预订内容!'));
			
			$yuyue_db->insert($viewdata);
			
			m_showmessage(L('预约成功!'),APP_PATH.'index.php?m=member&c=mobile&a=myyuyue');
		}else{
			include template('member', 'myuyue');
		}

	}	
	
	
	public function logout() {
		$setting = pc_base::load_config('system');
		param::set_cookie('auth', '');
		param::set_cookie('_userid', '');
		param::set_cookie('_username', '');
		param::set_cookie('_groupid', '');
		param::set_cookie('_nickname', '');
		param::set_cookie('cookietime', '');
		m_showmessage('注销成功', 'index.php?m=member&c=mobile');
	}
	
	public function myyuyue() {
		$memberinfo = $this->memberinfo;
		include template('member', 'myuyue_list');
	}
	
}

?>